package com.ls.uav_backend.controller;

import com.ls.uav_backend.common.BaseResponse;
import com.ls.uav_backend.common.ResultUtils;
import com.ls.uav_backend.exception.BusinessException;
import com.ls.uav_backend.exception.ErrorCode;
import com.ls.uav_backend.model.dto.CropGrowthGrowthIndicatorQueryDTO;
import com.ls.uav_backend.model.vo.crop.FieldLeafColorSumVO;
import com.ls.uav_backend.model.vo.crop.GrowthAnalysisResponseVO;
import com.ls.uav_backend.model.vo.crop.MaturityRateVO;
import com.ls.uav_backend.service.CropGrowthIndicatorsService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Set;

/**
 * 农作物生长指标表 控制层。
 *
 * @author <a href="https://gitee.com/Linkating">LinSkating</a>
 */
@RestController
@RequestMapping("/cropGrowthIndicators")
public class CropGrowthIndicatorsController {

    @Resource
    private CropGrowthIndicatorsService cropGrowthIndicatorsService;

    @PostMapping("/get/crop/data")
    public BaseResponse<GrowthAnalysisResponseVO> getCropData(@RequestBody CropGrowthGrowthIndicatorQueryDTO queryDTO) {
        if (queryDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }
        Long fieldId = queryDTO.getId();
        Date startEditTime = queryDTO.getStartEditTime();
        Date endEditTime = queryDTO.getEndEditTime();
        GrowthAnalysisResponseVO growthAnalysisData = cropGrowthIndicatorsService.getGrowthAnalysisData(fieldId, startEditTime, endEditTime);
        return ResultUtils.success(growthAnalysisData);
    }

    @GetMapping("/get/field/list")
    public BaseResponse<Set<Integer>> getFieldList() {
        Set<Integer> fieldList = cropGrowthIndicatorsService.getFieldIdSet();
        return ResultUtils.success(fieldList);
    }

    @GetMapping("/get/maturity/rate")
    public BaseResponse<MaturityRateVO> getMaturityRateData() {
        MaturityRateVO maturityRateData = cropGrowthIndicatorsService.getMaturityRateData();
        return ResultUtils.success(maturityRateData);
    }

    @GetMapping("/get/latest/color")
    public BaseResponse<FieldLeafColorSumVO> getLatestColor() {
        FieldLeafColorSumVO latestColor = cropGrowthIndicatorsService.selectLatestColor();
        return ResultUtils.success(latestColor);
    }

}
